package com.tjdx.projectsubmission.controller;

import com.tjdx.projectsubmission.vo.R;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@CrossOrigin
public class FileController {
    private String winPath = "D:\\outWorkCode\\FireriskChain\\image\\";
    private String linuxPath = "/root/image/";


    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.error();
        }

        try {
            // 获取文件的字节流
            byte[] bytes = file.getBytes();

            String fileName = file.getOriginalFilename();


            // 指定文件保存的路径
            String filePath = winPath + fileName;

            String os = System.getProperty("os.name");
            //Windows操作系统
            if (os != null && os.toLowerCase().startsWith("windows")) {
                filePath = winPath + fileName;
            } else if (os != null && os.toLowerCase().startsWith("linux")) {//Linux操作系统
                filePath = linuxPath + fileName;
            }


            // 创建文件对象
            File destination = new File(filePath);

            // 将文件写入磁盘
            file.transferTo(destination);

            return R.ok().data("data", fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/download/{filename}")
    public ResponseEntity<Resource> downloadFile(@PathVariable String filename) {
        // 指定文件所在目录
        String directory = winPath;

        String os = System.getProperty("os.name");
        //Windows操作系统
        if (os != null && os.toLowerCase().startsWith("windows")) {
            directory = winPath;
        } else if (os != null && os.toLowerCase().startsWith("linux")) {//Linux操作系统
            directory = linuxPath;
        }

        // 构造文件路径
        String filePath = directory + filename;

        try {
            // 创建Path对象表示文件路径
            Path path = Paths.get(filePath);

            // 创建Resource对象表示文件资源
            Resource resource = new UrlResource(path.toUri());

            if (resource.exists()) {
                // 设置响应头信息
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + resource.getFilename());

                // 返回文件资源的ResponseEntity
                return ResponseEntity.ok()
                        .headers(headers)
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }
}
